package net.sf.rose.business.bean.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.rose.business.service.ActionService;
import net.sf.rose.jdbc.service.Service;

import org.simpleframework.xml.Default;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

/**
 * @author fengjian
 * @since 2014-3-26
 */
@Default
@Root(name = "actionSet")
public class ActionSet extends Action {

	/** 所有的操作单元定义 */
	@ElementList(required = false)
	private List<String> actionUnits = new ArrayList<String>();

	/** 返回的常量属性 */
	@ElementList(name = "return-fields", required = false)
	private List<FieldBean> fields = new ArrayList<FieldBean>();

	public List<FieldBean> getFields() {
		return fields;
	}

	public void setFields(List<FieldBean> fields) {
		this.fields = fields;
	}

	public List<String> getActionUnits() {
		return actionUnits;
	}

	public void setActionUnits(List<String> actionUnits) {
		this.actionUnits = actionUnits;
	}

	//private List<Action> actionUnitBeans = null;
	public List<Action> getActionUnitBeans() {
		List<Action> list = new ArrayList<Action>();
		for(String key : this.actionUnits){
			list.add(ActionService.getAction(key));
		}
		return list;
	}

	/**
	 * 执行自定义操作
	 * @param request   - Http请求
	 * @param response  - Http返回
	 * @param service 	- 数据库连接服务类 
	 * @param map 		- 前台传过来的数据
	 * @param data      - 上下文返回的数据
	 */
	public void execute(HttpServletRequest request, HttpServletResponse response, Service service, 
			Map<String, Object> map, Map<String, Object> data){
		Map<String, Object> m = parseField(this.fields);
		map.putAll(m);
		data.putAll(m);
		Map<String, Action> units = ActionService.getAllActionUnit();
		for (String key : this.actionUnits) {
			Action unit = units.get(key);
			unit.execute(request, response, service, map, data);
		}
	}

}
